//
//  SendViewController.m
//  ZYShang
//
//  Created by wangguangming on 15/7/29.
//  Copyright © 2015年 Amber. All rights reserved.
//

#import "SendViewController.h"
#import "ReadyDiSignle.h"
#import "Base.h"
#import "SendTableViewCell.h"
#import "Order.h"
#import "SendDetailViewController.h"
@interface SendViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>
{
    int _from;
    int _to;
}
@property(nonatomic,strong)UITableView *mainTable;
@end

@implementation SendViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) 
        {
        self.view.backgroundColor=COLOR;
        }
    return self;
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
   
    

}
-(void)viewWillDisappear:(BOOL)animated
{
   
    [super viewWillDisappear:animated];
}
-(void)viewDidDisappear:(BOOL)animated
{
    
        //  
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBack];
    self.title=@"发货记录";
        //[[ReadyDiSignle signle] getSendworking];
    
    _from=0;
    _to=20;
    
    [[ReadyDiSignle signle] getSendworkingFrom:_from To:_to];
     [[ReadyDiSignle signle] addObserver:self forKeyPath:@"DeliveryArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
    self.mainTable=[[UITableView alloc] init];
    
    self.mainTable.frame=CGRectMake(0, 0, WIDTH, HEIGHT);
    self.mainTable.delegate=self;
    self.mainTable.dataSource=self;
    self.mainTable.separatorStyle = UITableViewCellSelectionStyleNone;
    self.mainTable.backgroundColor=COLOR;
    [self.view addSubview:self.mainTable];
    
    // Do any additional setup after loading the view.
}
-(void)observeValueForKeyPath:(nullable NSString *)keyPath ofObject:(nullable id)object change:(nullable NSDictionary *)change context:(nullable void *)context
{
    if ([keyPath isEqualToString:@"DeliveryArray"]) 
        {
        
        
       [[ReadyDiSignle signle] removeObserver:self forKeyPath:@"DeliveryArray"];
       
        [self.mainTable reloadData];
     
        }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [ReadyDiSignle signle].DeliveryArray.count;
}

  

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellid=@"sendcellid";
    
    Delivery *delivery=[ReadyDiSignle signle].DeliveryArray[indexPath.row];
    
    SendTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell==nil)
        {
        cell=[[SendTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
       }
    if (delivery.completed.intValue==0) 
        {
        cell.typeLaber.text=@"正在配送";
        cell.typeLaber.textColor=REDCOLOL;
        cell.backgroundColor=COLOR;
       }
    else
        {
        cell.typeLaber.text=@"配送完成";
        cell.typeLaber.textColor=BLUECOLOL;
        cell.backgroundColor=COLOR;
        }
    cell.senderLaber.text=[NSString stringWithFormat:@"送货员:%@(%@)",delivery.senderName,delivery.senderAcc];
    
    NSString *ss=@"";
    if (delivery.orders.count!=0)
        {
        for (int i=0; i<delivery.orders.count; i++)
            {
            
            Order *order=[[Order alloc] init];
            [order setValuesForKeysWithDictionary:delivery.orders[i]];
                // NSLog(@"===%@",order.oid);
            ss= [ss stringByAppendingString:[NSString stringWithFormat:@"%@ ",[order.oid substringFromIndex:order.oid.length-6]]];
            }
    

        }
        // [ss substringToIndex:ss.length-1];
    cell.itemLaber.text=[NSString stringWithFormat:@"订单号:%@",ss];
    cell.numberLaber.text=[NSString stringWithFormat:@"订单个数:%d个",delivery.orders.count];
//    NSDate *date1=[NSDate dateWithTimeIntervalSince1970:delivery.sentTime.intValue];
//    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];
    
    NSString *str=[NSString stringWithFormat:@"%lld",delivery.sentTime.longLongValue/1000];//开始时间戳
    NSTimeInterval time=[str doubleValue];//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate=[NSDate dateWithTimeIntervalSince1970:time];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *currentDateStr = [dateFormatter stringFromDate: detaildate];
    cell.beginLaber.text=[NSString stringWithFormat:@"发货时间:%@",currentDateStr];
    
    if(delivery.completeTime!=nil)
        {
    NSString *str1=[NSString stringWithFormat:@"%lld",delivery.completeTime.longLongValue/1000];//结束时间戳
    NSTimeInterval time1=[str1 doubleValue];//因为时差问题要加8小时 == 28800 sec
    NSDate *detaildate1=[NSDate dateWithTimeIntervalSince1970:time1];
        
        NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
            //设定时间格式,这里可以设置成自己需要的格式
        [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSString *currentDateStr1 = [dateFormatter stringFromDate: detaildate1];
        cell.endLaber.text=[NSString stringWithFormat:@"送达时间:%@",currentDateStr1];
    }
    else
        {
        cell.endLaber.text=[NSString stringWithFormat:@"送达时间:暂未送达"];
        }
           //实例化一个NSDateFormatter对象
    
    
   
     [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    cell.layer.borderColor=[[UIColor whiteColor] CGColor];
    cell.layer.borderWidth=3.0;
    
    
    return cell;
}
-(CGFloat)tableView:(nonnull UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    return 200;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
-(void)tableView:(nonnull UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    Delivery *delivery=[ReadyDiSignle signle].DeliveryArray[indexPath.row];
   
    SendDetailViewController *sendDetailViewController=[[SendDetailViewController alloc] init];
    sendDetailViewController.delivery=delivery;
    [self.navigationController pushViewController:sendDetailViewController animated:YES];
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView; 
{
    if (scrollView.contentOffset.y>[ReadyDiSignle signle].DeliveryArray.count*200-HEIGHT-40) 
        {
       
        _to+=20;
        
        [[ReadyDiSignle signle] getSendworkingFrom:_from To:_to];
        
         [[ReadyDiSignle signle] addObserver:self forKeyPath:@"DeliveryArray" options:NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld context:nil];
        [self.mainTable reloadData];
        }
    else
        {
        
        }


}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
